﻿unit Amy.Chromium.V8Handler;

interface

uses ceflib, System.SysUtils, System.Rtti, Winapi.Windows;

type
  TAmyUnmanagedWrapper = class(TCefv8ValueRef)
  end;

  TAmyPropertyAccessor = class(TCefV8AccessorOwn)
  protected
    function Get(const name: ustring; const obj: ICefv8Value;
      out value: ICefv8Value; const exception: ustring): Boolean; override;
    function Put(const name: ustring; const obj, value: ICefv8Value;
      const exception: ustring): Boolean; override;
  end;

  TAmyV8Handler = class(TCefv8HandlerOwn)
  private
    FContext: ICefv8Context;
  protected
    function Execute(const name: ustring; const obj: ICefv8Value;
      const arguments: TCefv8ValueArray; var retval: ICefv8Value;
      var exception: ustring): Boolean; override;
  public
    constructor Create; override;
    destructor Destroy; override;
  public
    class procedure CreateJavascriptMethods(Handler: ICefv8Handler; javascriptObject: ICefv8Value; methodNames: TArray<string>);
    class function Bind(context: ICefv8Context; name: string; obj: TValue; window: ICefV8Value): TAmyV8Handler;
  end;

implementation

  uses Log4D, Amy.Chromium.Core;

{ TAmyV8Handler }

class function TAmyV8Handler.Bind(context: ICefv8Context; name: string; obj: TValue; window: ICefV8Value): TAmyV8Handler;
var
  handler: TAmyV8Handler;
  javascriptWrapper: ICefv8Value;
  unmanagedWrapper: ICefv8Value;
  obj_instace: TObject;
  list: TArray<string>;
begin
  obj_instace := obj.AsClass.Create;
//unmanagedWrapper := TCefv8ValueRef.NewObject TAmyUnmanagedWrapper.Create(nil);
  javascriptWrapper := TCefv8ValueRef.NewObject(TAmyPropertyAccessor.Create);
//  javascriptWrapper.SetUserData(unmanagedWrapper);
  handler := TAmyV8Handler.Create;
  handler.FContext := context;
//  CreateJavascriptProperties();
  CreateJavascriptMethods(handler, javascriptWrapper, list);
  window.SetValueByKey(name, javascriptWrapper, []);

  Logger.Debug('  TAmyV8Handler.Bind>>>name: ' + name);
end;

constructor TAmyV8Handler.Create;
begin
  inherited;
end;

class procedure TAmyV8Handler.CreateJavascriptMethods(Handler: ICefv8Handler; javascriptObject: ICefv8Value; methodNames: TArray<string>);
var
  unmanagedWrapper: TAmyUnmanagedWrapper;
  nameStr: string;
  I: Integer;
begin
//  unmanagedWrapper := TAmyUnmanagedWrapper(javascriptObject.GetUserData.)
  for I := Low(methodNames) to High(methodNames) do
  begin
    nameStr := LowerCase(methodNames[I]);
    javascriptObject.SetValueByKey(nameStr, TCefv8ValueRef.NewFunction(nameStr, Handler), []);
  end;

  javascriptObject.SetValueByKey('age', TCefv8ValueRef.NewString('age hello'), [V8_PROPERTY_ATTRIBUTE_READONLY]);
  javascriptObject.SetValueByAccessor('age3', [V8_ACCESS_CONTROL_ALL_CAN_READ, V8_ACCESS_CONTROL_ALL_CAN_WRITE],
    [])
end;

destructor TAmyV8Handler.Destroy;
begin

  inherited;
end;

function TAmyV8Handler.Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
begin

end;

{ TAmyPropertyAccessor }

function TAmyPropertyAccessor.Get(const name: ustring; const obj: ICefv8Value; out value: ICefv8Value;
  const exception: ustring): Boolean;
begin

end;

function TAmyPropertyAccessor.Put(const name: ustring; const obj, value: ICefv8Value;
  const exception: ustring): Boolean;
begin

end;

end.
